##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Table 2: Charitable Giving Average Treatment Effects

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(plyr)
library(scales)

##################################################################

rm(list=ls())

##################################################################
# Treatment Effects: Charitable Giving 
##################################################################

data.working <-readRDS("d_r_cleaned_analysis_dataset.Rda")

dep.var <- c("q13ab.1","q13ab.2", "q13ab.3",  "q13ab.3.positive", "q13a.3", "q13a.3.positive", "q13a.3.amount")
# ab = pooled actual (selected for bonus) and hypothetical (not selected) bonus; 3 = international charity 2 = domestic charity; a = actual bonus winners

ind.group <- c("group3" , "group2")

reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
matrix <- vector("list", length(dep.var))

x=0 
for(i in 1:length(dep.var)){
	
	my.formula <-paste(dep.var[i],'~', paste(ind.group, collapse= ' + '))
	temp.model <- lm(my.formula, data=data.working)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	x = x+1
	se.models[[x]] <- temp.se
	reg.models[[x]] <- temp.model

}

varlabels <-       c("Information", "Salience", "Constant (Control Mean)" )
spectitle <- c("Charitable Giving")
outputfile <- c("tf_t_02_ate_charitable_giving.txt")
depvarcaption <- c("Charitable Giving")
cutoffs.star <- c(0.1, 0.02)
char.star = c("**", "***")
columnlabels <-c("Perc. Kept", "Perc. to US Charity","Perc. to Int. Charity", "Positive Int. Donation (1/0)", "Perc. to Int. Charity", "Positive Int. Donation (1/0)", "Amount to Int. Charity")

data.subset <- data.working[data.working$group2==1 | data.working$group3==1,] 
tresults.1 <- round(1-(t.test(q13ab.1~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.2 <- round((t.test(q13ab.2~group3, data=data.subset, alternative = c("less"))$p.value), digits=3)
tresults.3 <- round((t.test(q13ab.3~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.4 <- round((t.test(q13ab.3.positive~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.5 <- round((t.test(q13a.3~group3, data=data.subset, alternative = c("less"))$p.value), digits=3)
tresults.6 <- round((t.test(q13a.3.positive~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.7 <- round((t.test(q13a.3.amount ~group3, data=data.subset, alternative = c("less"))$p.value), digits=3)
lines.add <- list(c("Information-Salience (p-val.)", tresults.1,  tresults.2, tresults.3, tresults.4, tresults.5, tresults.6, tresults.7))

stargazer(reg.models[[1]], reg.models[[2]], reg.models[[3]], reg.models[[4]], reg.models[[5]], reg.models[[6]], reg.models[[7]],
          se=list(se.models[[1]], se.models[[2]], se.models[[3]], se.models[[4]], se.models[[5]], se.models[[6]], se.models[[7]]),
         type="text",
         title= spectitle,
          out=outputfile,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=TRUE,
          digits=2,
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = columnlabels,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
                  star.cutoffs = cutoffs.star,
     star.char = char.star,
         add.lines=lines.add
)   